/** @file    wAutomations_Automation.cpp
 *  @author  Alessandro Polo
 *  @version $Id: wAutomations_Automation.cpp 2891 2010-08-12 01:58:59Z alex $
 *  @brief
 * File containing methods for the wosh::gui::wAutomations_Automation class.
 * The header for this class can be found in wAutomations_Automation.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include "wAutomations_Automation.h"

 #include <core/ObjectAllocator.h>
 #include <framework/gui/GuiUtilities.h>

 #include "ui_wAutomations_Automation.h"


using namespace std;
namespace wosh {
 namespace gui {

 WOSH_REGISTER(wosh::gui::wAutomations_Automation, "wosh::gui::IWidgetSerializable", 1.0, _static_wAutomations_Automation )
 WOSH_GUI_MAP(wosh::gui::wAutomations_Automation, "wosh::automations::Automation", 0.0, _static_Map_wAutomations_Automation )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

wAutomations_Automation::wAutomations_Automation( QWidget* parent )
	: QWidget(parent) {

	this->m_ui = new Ui::wAutomations_Automation();
	this->m_ui->setupUi(this);

	setWindowIcon( GuiUtilities::createIconFor( getSerializableType() ) );
	setWindowTitle( getSerializableType() );

	connect(this->m_ui->cmbDebugLevel, SIGNAL(activated(const QString &)), this, SLOT(cmbDebugLevel_activated(const QString &)));

	this->m_ui->widgetProperties->init();

	loadObject(NULL);
 }

wAutomations_Automation::~wAutomations_Automation() {
	delete this->m_ui;
 }

void wAutomations_Automation::changeEvent(QEvent *e) {
	QWidget::changeEvent(e);
	if ( e->type() == QEvent::LanguageChange )
		this->m_ui->retranslateUi(this);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT wAutomations_Automation::loadObject( const wosh::automations::Automation* task ) {
	if ( task == NULL ) {
		setWindowIcon( GuiUtilities::createIconFor( getSerializableType() ) );
		setWindowTitle( getSerializableType() );

		this->m_ui->lblName->setText("");
		this->m_ui->lblType->setText("");
		this->m_ui->lblURI->setText("");

		this->m_ui->widgetProperties->setTargetURI( URI() );
		this->m_ui->widgetProperties->loadObject(NULL);
		this->m_ui->widgetGeometry->loadObject(NULL);

	 }
	else {
		setWindowIcon( GuiUtilities::createIconFor( task->getClassName() ) );
		setWindowTitle( QString("%1 [%2]").arg(task->getName().c_str()).arg(task->getClassName()) );

		this->m_ui->lblName->setText( task->getName().c_str() );
		this->m_ui->lblType->setText( task->getClassName() );
		this->m_ui->lblURI->setText( task->getURI().toString().c_str() );

		this->m_ui->widgetProperties->setTargetURI( task->getURI() );
		this->m_ui->widgetProperties->loadObject(&task->getProperties());

	 }
	return WRET_OK;
 }

WRESULT wAutomations_Automation::saveObject( wosh::automations::Automation* task ) const {
	if ( task == NULL ) return WRET_ERR_PARAM;
	return WRET_OK;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void wAutomations_Automation::cmbDebugLevel_activated(const QString& text) {
	List* dlProp = new List(2);
	dlProp->push_back<Variant>( _KEY_DebugLevel );
	dlProp->push_back<Variant>( string(text.toLatin1().data()) );
	Request* request_info = new Request( _METHOD_SetProperty, dlProp );
//	postMessageForTarget( new Message(request_info) );
//	this->m_ui->wUpBarInformation->showMessage("Changing DebugLevel..");
 }

 }; // namespace gui
}; // namespace wosh
